﻿using OnlineCourse.Model;
using OnlineCourse.Model.Entity;
using OnlineCourse.Services.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineCourse.Services
{
    public class SectionService : IServices.ISectionServices
    {
        private readonly ApplicationDbContext _db;
        public SectionService(ApplicationDbContext db)
        {
            this._db = db;
        }
        /// <summary>
        /// 添加单门课程详细信息
        /// </summary>
        /// <param name="courseId"></param>
        /// <param name="title"></param>
        /// <param name="type"></param>
        /// <param name="path"></param>
        /// <param name="detailed"></param>
        /// <param name="TryReading"></param>
        /// <returns></returns>
        public bool Add(Guid courseId, string title, int type, string path, string detailed, bool TryReading)
        {
            try
            {
                Section model = new Section();
                model.CourseId = courseId;
                model.Title = title;
                model.Type = type;
                model.Path = path;
                model.Detailed = detailed;
                model.TryReading = TryReading;
                this._db.Sections.Add(model);
                _db.SaveChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
        /// <summary>
        /// 删除单门课程
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(Guid id)
        {
            try
            {
                var m = _db.Sections.FirstOrDefault(p => p.Id == id);
                _db.Sections.Remove(m);
                _db.SaveChanges();
                return true;
            }
            catch (Exception err)
            {
                throw err;
            }
        }
        /// <summary>
        /// 修改单门课程信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="courseId"></param>
        /// <param name="title"></param>
        /// <param name="type"></param>
        /// <param name="path"></param>
        /// <param name="detailed"></param>
        /// <param name="TryReading"></param>
        /// <returns></returns>
        public bool Update(Guid id, Guid courseId, string title, int type, string path, string detailed, bool TryReading)
        {
            try
            {
                var m = _db.Sections.FirstOrDefault(p => p.Id == id);
                if (m != null)
                {
                    m.CourseId = courseId;
                    m.Title = title;
                    m.Type = type;
                    m.Path = path;
                    m.Detailed = detailed;
                    m.TryReading = TryReading;
                    _db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
